{% extends "base.html" %}
{% block content %}
        <h2>Chat with {{ recipient }}</h2>
        <div id="messages"></div>
        <div id="input">
            <input type="text" id="messageInput" placeholder="Type a message...">
            <button onclick="sendPrivateMessage()">Send</button>
        </div>
    <script src="https://cdn.socket.io/4.0.0/socket.io.min.js"></script>
    <script>
        const socket = io();
        const recipient = "{{ recipient }}";

        // Utility function to manage cookies
        function setCookie(name, value, days) {
            const date = new Date();
            date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
            document.cookie = `${name}=${encodeURIComponent(value)};expires=${date.toUTCString()};path=/`;
        }

        function getCookie(name) {
            const cookies = document.cookie.split('; ');
            for (let cookie of cookies) {
                const [key, value] = cookie.split('=');
                if (key === name) return decodeURIComponent(value);
            }
            return null;
        }

        // Join the private room
        socket.emit('join_private_room', { recipient });

        // Load chat history from the server
        fetch(`/chat_history/${recipient}`)
            .then(response => response.json())
            .then(data => {
                const messages = document.getElementById('messages');
                data.forEach(msg => {
                    const message = document.createElement('div');
                    message.innerHTML = `<strong>${msg.sender}:</strong> ${msg.content}`;
                    messages.appendChild(message);
                });
                messages.scrollTop = messages.scrollHeight;

                // Save chat history in cookies
                setCookie(`chat_${recipient}`, JSON.stringify(data), 7);
            });

        // Listen for incoming private messages
        socket.on('receive_private_message', (data) => {
            const messages = document.getElementById('messages');
            const message = document.createElement('div');
            message.innerHTML = `<strong>${data.sender}:</strong> ${data.message}`;
            messages.appendChild(message);
            messages.scrollTop = messages.scrollHeight;

            // Update chat history in cookies
            const chatHistory = JSON.parse(getCookie(`chat_${recipient}`) || '[]');
            chatHistory.push(data);
            setCookie(`chat_${recipient}`, JSON.stringify(chatHistory), 7);
        });

        // Send a private message
        function sendPrivateMessage() {
            const input = document.getElementById('messageInput');
            const message = input.value;
            if (message.trim()) {
                socket.emit('send_private_message', { recipient, message });
                input.value = '';
            }
        }

    </script>
    <style>

        #chat {
            max-width: 600px;
            margin: 50px auto;
            background: #fff;
            border-radius: 8px;
            box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
            overflow: hidden;
            width: 90%;
            max-width: 600px;
        }

        #messages {
            border-bottom: 1px solid #ddd;
            height: 300px;
            overflow-y: auto;
            padding: 15px;
            background: rgba(0, 0, 0, 0.3);
        }

        #messages div {
            margin-bottom: 10px;
            padding: 8px;
            border-radius: 5px;
            background: rgba(100, 100, 200, 0.1);
        }

        #input {
            display: flex;
            padding: 10px;
            background: rgba(255, 255, 255, 0.1);
            gap: 10px; /* Add spacing between input elements */
        }

        #input select, #input input {
            padding: 10px;
            border: 1px solid #ddd;
            border-radius: 4px;
            margin-right: 10px;
            flex: 2; /* Adjust flex ratio for better spacing */
        }

        form {
            background: #fff;
            padding: 20px;
            border-radius: 8px;
            box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
            width: 300px;
        }

        form h1 {
            margin-bottom: 20px;
            font-size: 24px;
            text-align: center;
        }

        label {
            display: block;
            margin-bottom: 5px;
            font-weight: bold;
        }

        input {
            width: 100%;
            padding: 10px;
            margin-bottom: 15px;
            border: 1px solid #ddd;
            border-radius: 4px;
        }

        p {
            text-align: center;
            margin-top: 10px;
        }

        p a {
            color: #007BFF;
            text-decoration: none;
        }

    </style>
{% endblock %}
